package com.example.myfirst.controller;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.example.myfirst.entity.Category;
import com.example.myfirst.entity.User;
import com.example.myfirst.service.CategoryService;
import com.example.myfirst.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("查询所有系列信息")
    @GetMapping
    public ResultVo getAllCategory() {
        List<Category> categoryList = categoryService.queryAllCategory();
        return new ResultVo(categoryList);
    }

    @ApiOperation("根据id查询系列信息")
    @GetMapping("/{id}")
    public ResultVo getCategoryById(@PathVariable Integer id) {
        List<Category> category = categoryService.queryCategoryById(id);
        return new ResultVo(category);
    }

    @ApiOperation("添加系列")
    @PostMapping
    public ResultVo addCategory(@RequestBody Category category) {
        Boolean flag = categoryService.addCategory(category);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("添加系列成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("添加系列失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("删除系列")
    @DeleteMapping("/{id}")
    public ResultVo delCategory(@PathVariable Integer id) {
        Boolean flag = categoryService.delCategory(id);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("删除系列成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("删除系列失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("根据id修改系列信息")
    @PutMapping("/{id}")
    public ResultVo updataCategoryById(@RequestBody Category category,@PathVariable Integer id) {
        Boolean flag = categoryService.updateCategoryById(category, id);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("修改系列成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("修改系列失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("根据店铺id来获取,店铺里面的系列")
    @GetMapping("/search")
    public  ResultVo queryCategoryByShopId(Integer shopId) {
        List<Category> categoryList = categoryService.queryCategoryByShopId(shopId);
        return new ResultVo(categoryList);
    }
}
